/*---------------------------------------------------------------------------------------------
 *  Copyright (c) Microsoft Corporation. All rights reserved.
 *  Licensed under the MIT License. See License.txt in the project root for license information.
 *--------------------------------------------------------------------------------------------*/
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
    return new (P || (P = Promise))(function (resolve, reject) {
        function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
        function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
        function step(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); }
        step((generator = generator.apply(thisArg, _arguments || [])).next());
    });
};
define(["require", "exports", "./workspaceEdit"], function (require, exports, workspaceEdit_1) {
    "use strict";
    Object.defineProperty(exports, "__esModule", { value: true });
    function getEditForCodeAction(client, action, vscode) {
        return action.changes && action.changes.length
            ? workspaceEdit_1.createWorkspaceEditFromFileCodeEdits(client, action.changes, vscode)
            : undefined;
    }
    exports.getEditForCodeAction = getEditForCodeAction;
    function applyCodeAction(client, action, vscode) {
        return __awaiter(this, void 0, void 0, function* () {
            const workspaceEdit = getEditForCodeAction(client, action, vscode);
            if (workspaceEdit) {
                if (!(yield vscode.workspace.applyEdit(workspaceEdit))) {
                    return false;
                }
            }
            return applyCodeActionCommands(client, action);
        });
    }
    exports.applyCodeAction = applyCodeAction;
    function applyCodeActionCommands(client, action) {
        return __awaiter(this, void 0, void 0, function* () {
            if (action.commands && action.commands.length) {
                for (const command of action.commands) {
                    const response = yield client.execute('applyCodeActionCommand', { command });
                    if (!response || !response.body) {
                        return false;
                    }
                }
            }
            return true;
        });
    }
    exports.applyCodeActionCommands = applyCodeActionCommands;
});
